package com.wgt.service.blog;

import javax.transaction.Transactional;

import com.wgt.domain.*;
import com.wgt.repository.BlogRepository;
import com.wgt.service.es.EsBlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;



/**
 * Blog 服务.
 * 
 * @since 1.0.0 2017年4月7日
 * @author <a href="https://waylau.com">Way Lau</a>
 */
@Slf4j
@Service
public class BlogServiceImpl implements BlogService {

	@Autowired
	private BlogRepository blogRepository;

	@Autowired
	private EsBlogService esBlogService;

	/**
	 * 保存文章
	 * @param blog
	 * @return
	 */
	@Transactional
	@Override
	public Blog saveBlog(Blog blog) {
		boolean isNew = blog.getId() == null ? true :false;
		EsBlog esBlog = null;

		Blog resultBlog = blogRepository.save(blog);
		if(isNew){
			esBlog = new EsBlog(blog);
		}else{
			esBlog = esBlogService.getEsBlogByBlogId(blog.getId());
			if(esBlog != null){
				log.info("获取到es对象>>>>[{}]",esBlog.toString());
				esBlog.update(resultBlog);
			}else{
				esBlog = new EsBlog(resultBlog);
			}

		}
		esBlogService.updateEsBlog(esBlog);
		return resultBlog;
	}

	/**
	 * 删除文章
	 * @param id
	 */
	@Transactional
	@Override
	public void removeBlog(Integer id) {
		blogRepository.delete(id);
		esBlogService.removeEsBlog(String.valueOf(id));
	}

	/**
	 * 更新文章内容
	 * @param blog
	 * @return
	 */
	@Transactional
	@Override
	public Blog updateBlog(Blog blog) {
		return this.saveBlog(blog);
	}

	/**
	 * 获取文章内容
	 * @param id
	 * @return
	 */
	@Override
	public Blog getBlogById(Integer id) {
		return blogRepository.findOne(id);
	}


	/**
	 *根据实践倒序进行排序
	 * @param user
	 * @param title
	 * @param pageable
	 * @return
	 */
	@Override
	public Page<Blog> listBlogsByTitleLike(User user, String title, Pageable pageable) {
		// 模糊查询
		title = "%" + title + "%";
		Page<Blog> blogs = blogRepository.findByUserAndTitleLikeOrderByCreateTimeDesc(user, title, pageable);
		return blogs;
	}

	/**
	 * 根据点赞量进行排序
	 * @param user
	 * @param title
	 * @param pageable
	 * @return
	 */
	@Override
	public Page<Blog> listBlogsByTitleLikeAndSort(User user, String title, Pageable pageable) {
		// 模糊查询
		title = "%" + title + "%";
		Page<Blog> blogs = blogRepository.findByUserAndTitleLike(user, title, pageable);
		return blogs;
	}

	/**
	 * 根据分类查询
	 * @param catalog
	 * @param pageable
	 * @return
	 */
	@Override
	public Page<Blog> listBlogsByCatalog(Catalog catalog, Pageable pageable) {
		Page<Blog> blogs = blogRepository.findByCatalog(catalog, pageable);
		return blogs;
	}

	/**
	 * 阅读量递增
	 * @param id
	 */
	@Override
	public void readingIncrease(Integer id) {
		Blog blog = blogRepository.findOne(id);
		blog.setReading(blog.getReading()+1);
		this.saveBlog(blog);
	}

	/**
	 * 创建评论
	 * @param blogId
	 * @param commentContent
	 * @return
	 */
	@Override
	public Blog createComment(Integer blogId, String commentContent) {
		Blog originalBlog = blogRepository.findOne(blogId);
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Comment comment = new Comment(user, commentContent);
		originalBlog.addComment(comment);
		return this.saveBlog(originalBlog);
	}

	/**
	 * 删除评论
	 * @param blogId
	 * @param commentId
	 */
	@Override
	public void removeComment(Integer blogId, Integer commentId) {
		Blog originalBlog = blogRepository.findOne(blogId);
		originalBlog.removeComment(commentId);
		this.saveBlog(originalBlog);
	}

	/**
	 * 点赞
	 * @param blogId
	 * @return
	 */
	@Override
	public Blog createVote(Integer blogId) {
		Blog originalBlog = blogRepository.findOne(blogId);
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Vote vote = new Vote(user);
		boolean isExist = originalBlog.addVote(vote);
		if (isExist) {
			throw new IllegalArgumentException("该用户已经点过赞了");
		}
		return this.saveBlog(originalBlog);
	}

	/**
	 * 取消赞
	 * @param blogId
	 * @param voteId
	 */
	@Override
	public void removeVote(Integer blogId, Integer voteId) {
		Blog originalBlog = blogRepository.findOne(blogId);
		originalBlog.removeVote(voteId);
		this.saveBlog(originalBlog);
	}
}
